package com.dbh.alg.algorithm.recursion;

import java.util.Arrays;

/**
 * 递归-斐波那契数列
 * @author 董帮辉
 * @date 2025/4/19
 **/
public class E06Fibonacci {

    public static int fibonacci(int n) {
        int[] cache = new int[n + 1];
        Arrays.fill(cache, -1);
        cache[0] = 0;
        cache[1] = 1;
        return f(n, cache);
    }

    private static int f(int n, int[] cache) {
        if (cache[n] != -1) {
            return cache[n];
        }
        int x = f(n - 1, cache);
        int y = f(n - 2, cache);
        cache[n] =  x + y;
        return cache[n];
    }
}
